package com.tpp.scheduler.rabbit;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.tpp.comm.pojo.TpSchedulerSeat;
import com.tpp.comm.pojo.TpTask;
import com.tpp.comm.util.TpConst;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.tpp.scheduler.feign.OrderFeign;
import com.tpp.scheduler.mapper.SchedulerMapper;
import com.tpp.scheduler.mapper.SchedulerSeatMapper;
import com.tpp.scheduler.mapper.TaskMapper;
import com.tpp.scheduler.vo.RabbitOrderMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Component
@Slf4j
@RabbitListener(queues=TpConst.Rabbit.ORDER_DEAD_QUEUE)
public class DeadOrderRabbitConsumer {

    @Resource
    private TaskMapper taskMapper;

    @Resource
    private SchedulerSeatMapper schedulerSeatMapper;

    @Resource
    private SchedulerMapper schedulerMapper;

    @Resource
    private RedisTemplate<String,String> stringRedisTemplate;

    @Resource
    private ObjectMapper objectMapper;

    @Resource
    private OrderFeign orderFeign;

    @RabbitHandler
    public void deadOrderConsumer(TpTask task, @Header(AmqpHeaders.DELIVERY_TAG) long tag, Channel channel){
        //处理失效订单
        TpTask tpTask = taskMapper.selectOne(new LambdaQueryWrapper<TpTask>().eq(TpTask::getOrderNo,task.getOrderNo()));
        log.info("失效订单处理:{}:{}",task,tag);
        try {
            orderFeign.updateOrderStatus(task.getOrderNo(),-1);
            RabbitOrderMessage orderMessage = objectMapper.readValue(tpTask.getRequestBody(), RabbitOrderMessage.class);
            //获取座位信息
            String[] seatStr = orderMessage.getSeatPositions().split(",");
            //解锁
            BoundSetOperations<String, String> seatsLock = stringRedisTemplate.boundSetOps(TpConst.Redis.STOPE_SCHEDULED_USEDSEAT+":"+orderMessage.getSchedulerId());
            for (String s : seatStr) {
                seatsLock.remove(s);
            }
            //修改状态
            LambdaQueryWrapper<TpSchedulerSeat> schedulerSeatWrapper = Wrappers.lambdaQuery();
            schedulerSeatWrapper.eq(TpSchedulerSeat::getOrderNo,orderMessage.getOrderNo());
            List<TpSchedulerSeat> tpSchedulerSeats = schedulerSeatMapper.selectList(schedulerSeatWrapper);
            tpSchedulerSeats.stream().forEach((tpSchedulerSeat)->{
                tpSchedulerSeat.setOrderNo("");
                tpSchedulerSeat.setUserId(null);
                tpSchedulerSeat.setStatus(1);
                tpSchedulerSeat.setId(tpSchedulerSeat.getId());
                schedulerSeatMapper.updateById(tpSchedulerSeat);
            });
            channel.basicAck(tag,true);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
